package com.huyi.common.annotation.aspect;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * 通用处理拦截api接口打印接口log日志
 * 
 * @author shengli.hu
 */
@Slf4j
@Aspect
@Component
public class APIRequestAspect {
	
    private long startTimeMillis = 0; // 开始时间  

	@Pointcut("execution(* com.huyi.web.controller.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public void doPointcut() {
	}

	/**
	 * 方法之前执行
	 * @throws ClassNotFoundException 
	 * 
	 * @see
	 */
	@Before("doPointcut()")
	public void beforeAspect(JoinPoint point) {
		startTimeMillis = System.currentTimeMillis(); //记录方法开始执行的时间 
		MethodSignature methodSignature = (MethodSignature) point.getSignature();
		String methodName = methodSignature.getName();
		String className = point.getTarget().getClass().getSimpleName();
		log.info("【api接口】调用{}.{}方法开始,参数：{} ", className, methodName, Arrays.toString(point.getArgs()));
	}

	/**
	 * 方法之后执行
	 * 
	 * @see
	 */
	@After("doPointcut()")
	public void afterAspect(JoinPoint point) {
		MethodSignature methodSignature = (MethodSignature) point.getSignature();
		String methodName = methodSignature.getName();
		String className = point.getTarget().getClass().getSimpleName();
		long end = System.currentTimeMillis();
		log.info("【api接口】调用{}.{}方法结束,耗时usetime={}ms.", className, methodName, (end - startTimeMillis));
	}
}